package com.yanq.liancar.fragment;


import android.annotation.SuppressLint;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.gson.Gson;
import com.gyf.barlibrary.ImmersionBar;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;

import com.yanq.liancar.R;
import com.yanq.liancar.adapter.MarketAdapter;
import com.yanq.liancar.base.BaseFragment;
import com.yanq.liancar.base.BaseLYFragment;
import com.yanq.liancar.base.BaseLazyFragment;
import com.yanq.liancar.bean.BaseBean;
import com.yanq.liancar.bean.GoShopBean;
import com.yanq.liancar.bean.MyBanner;
import com.yanq.liancar.bean.UserBean;
import com.yanq.liancar.url.Config;
import com.yanq.liancar.util.Constant;
import com.yanq.liancar.util.GlideImageLoader;
import com.yanq.liancar.util.LogUtils;
import com.yanq.liancar.util.ToastUtil;
import com.yanq.liancar.util.ToastUtils;
import com.yanq.liancar.view.VerticalSwipeRefreshLayout;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.youth.banner.listener.OnBannerListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * A simple {@link Fragment} subclass.
 */
public class MarketFragment extends BaseLazyFragment {


    @BindView(R.id.top)
    TextView toolbar;
    @BindView(R.id.webView)
    WebView webView;
    private String url;
   // protected ImmersionBar mImmersionBar;
    private String jifen;
    public static MarketFragment newInstance(String param1) {
        MarketFragment fragment = new MarketFragment();
        Bundle args = new Bundle();
        args.putString("agrs1", param1);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onResume() {
        super.onResume();
        url="http://llc.value19.com/jifen.php"+"?apid="+UserBean.getID(mActivity) +"&name="+UserBean.getName(mActivity)+"&type=1";
        setWebData(url);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
    }

    @Override
    protected void initImmersionBar() {
        super.initImmersionBar();
        ImmersionBar.with(mActivity).statusBarDarkFont(true).init();
    }

    @Override
    protected int setLayoutId() {
        return R.layout.fragment_market;
    }
//
//    @Override
//    protected void onCreateViewLazy(Bundle savedInstanceState) {
//        super.onCreateViewLazy(savedInstanceState);
//        setContentView(R.layout.fragment_market);
//
//        ImmersionBar.with(mActivity).statusBarColor(R.color.white).statusBarDarkFont(true).init();
//
//
//        LogUtils.e("111111111111","MarketFragment======onCreateViewLazy");
//    }
//
//    @Override
//    protected void onResumeLazy() {
//        super.onResumeLazy();
//        url="http://llc.value19.com/jifen.php"+"?apid="+UserBean.getID(mActivity) +"&name="+UserBean.getName(mActivity)+"&type=1";
//        setWebData(url);
//        LogUtils.e("111111111111","MarketFragment======onResumeLazy");
//    }

    @SuppressLint("SetJavaScriptEnabled")
    private void setWebData(String url) {

        ToastUtil.showProgressDialog(mActivity);
        WebSettings setting = webView.getSettings();
        setting.setJavaScriptEnabled(true);
        setting.setBlockNetworkImage(false);
        setting.setSupportZoom(true);
        setting.setUseWideViewPort(true);
        setting.setAllowFileAccess(true);
        setting.setCacheMode(WebSettings.LOAD_NO_CACHE);
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
            setting.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
        }
        webView.loadUrl(url);
        webView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                ToastUtil.dismissProgressDialog();
            }

            @Override
            public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
                super.onReceivedError(view, errorCode, description, failingUrl);
                ToastUtil.dismissProgressDialog();
            }
        });
    }


//    private void goShop(){
//            OkGo.<String>get("http://llc.value19.com/jifen.php")
//                    .tag(this)
//                    .params("apid", UserBean.getcToken(mActivity))
//                    .params("type", 1)
//                    .params("name", UserBean.getName(mActivity))
//                    .params("jifen",jifen )
//                    .execute(new StringCallback() {
//                        @Override
//                        public void onSuccess(Response<String> response) {
//                            Gson gson = new Gson();
//                            GoShopBean baseBean=gson.fromJson(response.body(),GoShopBean.class);
//                            if (baseBean.getCode()==2){
////                                url=baseBean.getMsg();
////                                if (url != null) {
////                                    setWebData(url);
////                                }
//                            }else {
//                                RxToast.error(baseBean.getMsg());
//                            }
//
//                        }
//
//                        @Override
//                        public void onError(Response<String> response) {
//                            super.onError(response);
//                            RxToast.error("服务器连接错误");
//                        }
//                    });
//
//    }
}
